<?php
$adm_menu = "1";
include("config.php");
if (!isset($_SESSION["auser"]) || $_SESSION["auser"] == "" || $_SESSION['agroup'] != "WEB_ADMIN") 
{
header('Location: index.php');
die();
}

$db_rdata = new DB($rdata_server_username,$rdata_server_pass,$rdata_server_db,$rdata_server,$rdata_driver);
$db_www = new DB($server_username,$server_pass,$server_db,$server,$driver);

if (isset($_REQUEST["act"])){
if ($_REQUEST["act"] == "unblockc" && isset($_REQUEST["nr"])) {
		$sql = "exec web_change_card_status "
		. "@type='TIEK_BLOK',"
		. "@action='UNBLOCK',"
		. "@imkodas='',"
		. "@kortele='".iconv('UTF-8','windows-1257', addslashes_mssql($_REQUEST["nr"]))."'";		
		$db_www->execute($sql);	
		
		logAction("Tiekėjas atblokuoja:" . $_REQUEST["nr"],2);
	} else if ($_REQUEST["act"] == "blockc" && isset($_REQUEST["nr"])) {
		$sql = "exec web_change_card_status "
		. "@type='TIEK_BLOK',"
		. "@action='BLOCK',"
		. "@imkodas='',"
		. "@kortele='".iconv('UTF-8','windows-1257', addslashes_mssql($_REQUEST["nr"]))."'";		
		$db_www->execute($sql);	

		logAction("Tiekėjas užblokuoja:" . $_REQUEST["nr"],2);	
	}
	}

include("header.php");
include("menu.php");


	if (isset($_REQUEST['active'])){
		$active = $_REQUEST['active'];
		$_SESSION["cardsactive"]=$active;
	} else {
	    if (isset($_SESSION["cardsactive"])) {
		  $active = $_SESSION["cardsactive"];
		} else {
		  $active = "1";
		}
	} 


	if (isset($_REQUEST['uid'])){
		$uid = $_REQUEST['uid'];
		$_SESSION["cardsuid"]=$uid;
	} else {
	    if (isset($_SESSION["cardsuid"])) {
		  $uid = $_SESSION["cardsuid"];
		} else {
		  $uid = "";
		}
	} 
	if (isset($_REQUEST['pav'])){
		$pav = $_REQUEST['pav'];
        $_SESSION["cardspav"]=$pav;
	} else {
	   if (isset($_SESSION["cardspav"])) {
		  $pav = $_SESSION["cardspav"];
		} else {
		   $pav = "";
		}
	} 
	if (isset($_REQUEST['cdesc'])){
		$cdesc = $_REQUEST['cdesc'];
		$_SESSION["cardsdesc"]=$cdesc;
	} else {
	    if (isset($_SESSION["cardsdesc"])) {
		  $cdesc = $_SESSION["cardsdesc"];
		} else {
		   $cdesc = "";
		}
	} 	
	if (isset($_REQUEST['cnumber'])){
		$cnumber = $_REQUEST['cnumber'];
		$_SESSION["cardsnumber"]=$cnumber;
	} else {
	    if (isset($_SESSION["cardsnumber"])) {
		  $cnumber = $_SESSION["cardsnumber"];
		} else {
		  $cnumber = "";
		}
	} 	
	if (isset($_REQUEST['active'])){
		$active = $_REQUEST['active'];
		$_SESSION["cardsactive"]=$active;
	} else {
	    if (isset($_SESSION["cardsactive"])) {
		  $active = $_SESSION["cardsactive"];
		} else {
		  $active = "";
		}
	} 

?>
<script>

function status(n,nr)
{
    if (n == "1") {
		var answer = confirm("Ar blokuoti kortelę?")
		if (answer){
			window.location='acards.php?act=blockc&nr='+nr ;
		}
	} else {
		var answer = confirm("Ar atblokuoti kortelę?")
		if (answer){
			window.location='acards.php?act=unblockc&nr='+nr ;
		}
	}	
}

var options, a;
jQuery(function(){
	options = { serviceUrl:'clients_info.php',	
				maxHeight:200,
				width:300 };
	a = $('#query').autocomplete(options);
}); 

	$(document).ready(function(){	

		if (!$.browser.opera) {
    
			// select element styling
			$('select.select').each(function(){
				var title = $(this).attr('title');
				if( $('option:selected', this).val() != ''  ) title = $('option:selected',this).text();
				$(this)
					.css({'z-index':10,'opacity':0,'-khtml-appearance':'none'})
					.after('<span class="select">' + title + '</span>')
					.change(function(){
						val = $('option:selected',this).text();
						$(this).next().text(val);
						})
			});

		};
		
	});
</script>
<table class="innercont" cellspacing="0" cellpadding="10">
<tr><td class="leftcol">
<table cellspacing="0" cellpadding="5" class="params" style="">
<tr>
<td style="text-align:center;"><a id="newButton" href="acard.php" class="card3">Nauja</a></td>
</tr>
</table>
<br>
	<form method='get'><table cellspacing='0' cellpadding='5' class='params'>
	
     <tr><th>Kliento ID:</th></tr>
	<tr><td style='text-align:left;'><input  class="inp" type='text' name="uid" value="<?php echo htmlspecialchars($uid);?>"></td></tr>
     <tr><th>Kliento pavadinimas:</th></tr>
	<tr><td style='text-align:left;'><input id="query" autocomplete="off" class="inp" type='text' name="pav" value="<?php echo htmlspecialchars($pav);?>"></td></tr>
     <tr><th>Kortelės pavadinimas:</th></tr>
	<tr><td style='text-align:left;'><input  class="inp" type='text' name="cdesc" value="<?php echo htmlspecialchars($cdesc);?>"></td></tr>
     <tr><th>Kortelės numeris:</th></tr>
	<tr><td style='text-align:left;'><input  class="inp" type='text' name="cnumber" value="<?php echo htmlspecialchars($cnumber);?>"></td></tr>
		<tr><th>Korteles statusas:</th></tr>
	<tr><td style='text-align:left;'>
        <div>
        <select name="active" class="select" >
		<option value="1" <?php if ($active=="1") {echo 'selected="selected"';}?>>Visos</option>
		<option value="3" <?php if ($active=="3") {echo 'selected="selected"';}?>>Aktyvios</option>
		<option value="4" <?php if ($active=="4") {echo 'selected="selected"';}?>>Blokuotos</option>

		<option value="0" <?php if ($active=="0") {echo 'selected="selected"';}?>>Anuliuotos</option>

		</select>
        </div>
	</td></tr>

	<tr><td ><input class='but bgfilter' type = "submit" name='submit' value = "Rodyti"></td></tr>
	</table></form>

</td><td class="rightcol" rowspan="2">
<?php

		if (isset($_REQUEST['submit']) ||isset($_REQUEST['act'])) {
			$allcards_query = str_replace("<uid>", addslashes_mssql($uid), $allcards_query);
			$allcards_query = str_replace("<pav>", addslashes_mssql(@iconv('UTF-8','windows-1257',$pav)), $allcards_query);
			$allcards_query = str_replace("<cdesc>", addslashes_mssql(@iconv('UTF-8','windows-1257',$cdesc)), $allcards_query);
			$allcards_query = str_replace("<cnumber>", addslashes_mssql($cnumber), $allcards_query);
			if (!isset($active) || $active == "1" || $active == "") {
				$allcards_query = str_replace("<aktyvios>", $korteliu_prefiksas, $allcards_query);
			} else if ($active == "0"){
				$allcards_query = str_replace("<aktyvios>", "SUBSTRING(N51_PAV,1,1)='_' ", $allcards_query);
			}else if ($active == "3"){
				$allcards_query = str_replace("<aktyvios>", $korteliu_prefiksas. " and n51_viskas=0 ", $allcards_query);
			}else if ($active == "4"){
				$allcards_query = str_replace("<aktyvios>", $korteliu_prefiksas . " and n51_viskas=1 ", $allcards_query);
			}
			$cards = $db_www->returnList($allcards_query);	
			//echo $allcards_query;
			
			?>
<table cellspacing="0" cellpadding="3" class="innercont3 width100pr">
<?php


	echo "<tr><th>Kliento pavadinimas</th><th nowrap>Kortelės nr.</th><th>Pavadinimas</th><th>Blokavimo priežastis</th><th>Blok</th></tr>";
	foreach ($cards as $card) {
		$uid = @iconv('windows-1257', 'UTF-8', trim($card["n60_kodas_sm"]));
		
			echo "<tr>";
			echo "<td><A href='user.php?ret=acards&code=".$uid."' class='card'>".@iconv('windows-1257', 'UTF-8', htmlspecialchars(substr($card["n60_pav2"],0,33))). "</A></td>";					
			echo "<td><A href='acard.php?uid=".@iconv('windows-1257', 'UTF-8', trim($card["N51_KODAS_KT"]))."' class='card'>".trim(substr($card["N51_PAV"],0,19))."</A></td>";
			echo "<td>".@iconv('windows-1257', 'UTF-8', htmlspecialchars(substr($card["N51_KORTA"],0,33))). "</td>";
			echo "<td>".@iconv('windows-1257', 'UTF-8', htmlspecialchars(substr($card["n35_pav"],0,33))) ."</td>";
			echo "<td>";
			if ($card["N51_VISKAS"] == "0") {
				echo "<A onclick=\"status('1','".@iconv('windows-1257', 'UTF-8', trim($card["N51_KODAS_KT"]))."');\"><img src='img/green_new.gif' alt='' title=''></A>";
			} else {
				echo "<A onclick=\"status('0','".@iconv('windows-1257', 'UTF-8', trim($card["N51_KODAS_KT"]))."');\"><img src='img/red_new.gif' alt='' title=''></A>";
			}
			echo "</td>";
			echo "</tr>\n";
		
	}	



?>
</table>			
			
			<?php
		}

?>
</td></tr>
<tr><td >

</td></tr>
</table>
<?php
include("footer.php");
?>